﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ICSharpCode.SharpZipLib.Zip;

namespace XamlingCore.WP.Infrastructure.Compression
{
    public class Zip
    {
        public static List<string> ExtractFiles(Stream result, string baseDirectory)
        {
            List<string> filesWritten = new List<string>();

            using (var s = new ZipInputStream(result))
            {
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    if (!string.IsNullOrEmpty(System.IO.Path.GetFileName(theEntry.Name)))
                    {
                        var basefileName = theEntry.Name.Replace("CA:/", "");
                        basefileName = basefileName.Replace("base/", "");
                        string changedFileName = System.IO.Path.GetFileName(basefileName);

                        var fullFileName = String.Format("{0}/{1}/{2}", baseDirectory, System.IO.Path.GetDirectoryName(basefileName), changedFileName);

                        fullFileName = fullFileName.Replace("//", "/");
                        fullFileName = fullFileName.Replace("/", "\\");
                        //fullFileName = "\\" + fullFileName;
                        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            var dirToCreate = System.IO.Path.GetDirectoryName(fullFileName);
                            if (dirToCreate != null)
                            {
                                if (!isf.DirectoryExists(dirToCreate))
                                {
                                    isf.CreateDirectory(dirToCreate);
                                }
                            }
                            using (
                                IsolatedStorageFileStream fs = new IsolatedStorageFileStream(fullFileName,
                                                                                             System.IO.FileMode.Create, isf)
                                )
                            {
                                int size = 2048;
                                byte[] data = new byte[2048];
                                while (true)
                                {
                                    size = s.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        fs.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                fs.Close();
                                filesWritten.Add(changedFileName);
                            }
                        }
                    }
                }

            }
            return filesWritten;
        }
    }
}
